package com.hxd.otc.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.flyco.dialog.widget.base.BaseDialog;
import com.hxd.otc.R;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.ExValidType;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.BlocksToast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 交易方式密码或验证码输入框
 */
public class ExValidTypeInputDialog extends BaseDialog<ExValidTypeInputDialog> {
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.et_ver_code)
    EditText mEtVerCode;
    @BindView(R.id.tv_send_ver_code)
    TextView mTvSendVerCode;
    private ExValidType mExValidType;

    private OnExValidTypeListener onExValidTypeListener;

    public ExValidTypeInputDialog(Context context) {
        super(context);
    }

    @Override
    public View onCreateView() {
        widthScale(0.67f);
        setCanceledOnTouchOutside(false);
        return View.inflate(getContext(), R.layout.dialog_ex_valid_type_input, null);
    }

    @Override
    public void onViewCreated(View view) {
        ButterKnife.bind(this, view);
        mTvTitle.setText(R.string.modify_verification_method);
    }

    public TextView getmTvSendVerCode() {
        return mTvSendVerCode;
    }

    @Override
    public void setUiBeforShow() {
        mEtVerCode.setText("");
        if (ExValidType.GoogleAuth.equals(mExValidType)) {
            mEtVerCode.setHint(R.string.please_input_your_google_code);
            UIUtils.setGoogleCodeEditText(mEtVerCode);
        } else if (ExValidType.SMS.equals(mExValidType)) {
            mEtVerCode.setHint(R.string.please_input_your_phone_ver_code);
            UIUtils.setPhoneVerCodeEditText(mEtVerCode);
        } else if (ExValidType.Email.equals(mExValidType)) {
            mEtVerCode.setHint(R.string.please_input_your_email_ver_code);
            UIUtils.setEmailVerCodeEditText(mEtVerCode);
        }
    }

    public ExValidTypeInputDialog setExValidType(ExValidType exValidType) {
        mExValidType = exValidType;
        return this;
    }

    public ExValidTypeInputDialog setOnExValidTypeListener(OnExValidTypeListener onExValidTypeListener) {
        this.onExValidTypeListener = onExValidTypeListener;
        return this;
    }

    @OnClick({R.id.tv_cancel, R.id.tv_confirm, R.id.tv_send_ver_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_cancel:
                dismiss();
                break;
            case R.id.tv_confirm:
                onConfirm();
                break;
            case R.id.tv_send_ver_code:
                ExValidType exValidType = UserHelper.getExValidType();
                if (ExValidType.GoogleAuth.equals(exValidType)) {
                    UIUtils.setGoogleCodeEditText(mEtVerCode);
                } else if (ExValidType.SMS.equals(exValidType)) {
                    UIUtils.setPhoneVerCodeEditText(mEtVerCode);
                    if (onExValidTypeListener != null) {
                        onExValidTypeListener.getVerCode(exValidType);
                    }
                } else if (ExValidType.Email.equals(exValidType)) {
                    UIUtils.setEmailVerCodeEditText(mEtVerCode);
                    if (onExValidTypeListener != null) {
                        onExValidTypeListener.getVerCode(exValidType);
                    }
                }
                break;
        }
    }

    private void onConfirm() {

        String verCode = mEtVerCode.getText().toString().trim();
        if (TextUtils.isEmpty(verCode)) {
            BlocksToast.get().showToast(mEtVerCode.getHint().toString());
            return;
        }

        if (onExValidTypeListener != null) {
            onExValidTypeListener.onVerCode(verCode);
        }
        dismiss();
    }

    public interface OnExValidTypeListener {
        void onVerCode(String content);//点击确认按钮 调用

        void getVerCode(ExValidType exValidType);// 点击 获取验证码按钮
    }
}
